<template>
  <div>
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
          v-model="beforPhone"
          name="beforPhone"
          @change="valueTest"
          placeholder="原手机号"
          :rules="[
            { validator: validatorRegular, message: '请输入正确的手机号' },
          ]"
        />
        <van-field
          v-model="newPhone"
          name="newPhone"
          @change="valueTest"
          placeholder="新手机号"
          :rules="[{ validator, message: '请输入新的手机号' }]"
        />
      </van-cell-group>
      <div style="margin: 16px">
        <van-button
          round
          block
          type="primary"
          :disabled="isDisabled"
          native-type="submit"
        >
          提交
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { Toast } from "vant";
export default {
  data() {
    return {
      beforPhone: "",
      newPhone: "",
      isDisabled: true,
    };
  },
  methods: {
    validatorRegular() {
      return /^(((13[0-9])|(14[5-7])|(15[0-9])|(17[0-9])|(18[0-9]))+\d{8})$/.test(
        this.beforPhone
      );
    },
    validator() {
      if (this.beforPhone == this.newPhone) {
        // console.log("两次手机号对比");
        return false;
      } else {
        // console.log(
        //   "新手机号验证",
        //   /^(((13[0-9])|(14[5-7])|(15[0-9])|(17[0-9])|(18[0-9]))+\d{8})$/.test(
        //     this.newPhone
        //   )
        // );
        if (
          /^(((13[0-9])|(14[5-7])|(15[0-9])|(17[0-9])|(18[0-9]))+\d{8})$/.test(
            this.newPhone
          )
        ) {
          return true;
        } else if (this.newPhone == this.beforPhone) {
          return true;
        } else {
          return false;
        }
      }
    },
    valueTest() {
      if (
        this.beforPhone != this.newPhone &&
        this.beforPhone.length == 11 &&
        this.newPhone.length == 11
      ) {
        this.isDisabled = false;
        // console.log("通过 ", this.password.length);
      } else {
        // console.log("未通过 ");
        this.isDisabled = true;
      }
    },
    onSubmit() {
      let Consistent = this.validator();
      if (Consistent) {
        // console.log("shoujihaobuyiyang", this.beforPhone, this.newPhone);

        this.$http({
          url: "/driver/updatePhone",
          method: "POST",
          params: {
            beforePhone: this.beforPhone,
            nowPhone: this.newPhone,
          },
        })
          .then((res) => {
            // console.log("请求成功", res);
            if (res.data.code == 1) {
              Toast(res.data.msg);
              setTimeout(() => {
                this.$router.push("/");
              }, 1000);
            }
          })

          .catch((err) => {
              Toast('网络连接超时')          
          // console.log("请求失败", err);
          });
      }
    },
  },
};
</script>